# -*- coding: utf-8 -*-

from jsonschema import validate
from jsonschema import ValidationError

from staffmanager.common import exception


# 参数校验装饰器
def json_validate(schema):
    def wrapper(func):
        def inner(obj, data, *args, **kwargs):
            try:
                validate(data, schema)
            except ValidationError as e:
                print("参数校验失败：{}!".format(e.message))
                raise exception.IllegalParamEXception

            else:
                return func(obj, data, *args, **kwargs)
        return inner
    return wrapper


schema_update = {
    "type": "object",
    "required": ["id"],
    "additionalProperties": False,
    "properties": {
        "name": {
            "type": "string",
            "minLength": 1,
            "maxLength": 20
        },
        "id": {
            "type": "integer",
            "minimum": 1,
            "maximum": 2000
        },
        "entry_date": {
            "type": "string",
            "minLength": 1,
            "maxLength": 10
        },
        "department": {
            "type": "string",
            "minimum": 1,
            "maximum": 20
        },
        "position": {
            "type": "string",
            "minimum": 1,
            "maximum": 20
        },
    }
}


schema_add = {
    "type": "object",
    "required": ["name", "id", "entry_date", "department", "position"],
    "additionalProperties": False,
    "properties": {
        "name": {
            "type": "string",
            "minLength": 1,
            "maxLength": 20
        },
        "id": {
            "type": "integer",
            "minimum": 1,
            "maximum": 2000
        },
        "entry_date": {
            "type": "string",
            "minLength": 1,
            "maxLength": 10
        },
        "department": {
            "type": "string",
            "minimum": 1,
            "maximum": 20
        },
        "position": {
            "type": "string",
            "minimum": 1,
            "maximum": 20
        },
    }
}

schema_del_get = {
    "type": "object",
    "required": ["id"],
    "additionalProperties": False,
    "properties": {
        "id": {
            "type": "integer",
            "minimum": 1,
            "maximum": 2000
        }
    }
}


schema_list = {
    "type": "object",
    "additionalProperties": False,
    "properties": {
        "name": {
            "type": "string",
            "minLength": 1,
            "maxLength": 20
        },
        "id": {
            "type": "integer",
            "minimum": 1,
            "maximum": 2000
        },
        "entry_date": {
            "type": "string",
            "minLength": 1,
            "maxLength": 10
        },
        "department": {
            "type": "string",
            "minimum": 1,
            "maximum": 20
        },
        "position": {
            "type": "string",
            "minimum": 1,
            "maximum": 20
        },
        "sort": {
            "type": "string",
            "minimum": 1,
            "maximum": 20
        },
    }
}